package org.example.admin.controller;

import io.swagger.annotations.Api;
import org.example.admin.dto.GoodsAllinone;
import org.example.admin.dto.GoodsOrderSpDto;
import org.example.admin.service.AdminGoodsService;
import org.example.common.utils.ResponseUtil;
import org.example.db.entity.Goods;
import org.example.db.entity.GoodsAttribute;
import org.example.db.entity.GoodsOrder;
import org.example.db.entity.GoodsSpecification;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
@Api(tags = "对商品的操作")
public class AdminGoodsController {

    AdminGoodsService adminGoodsService;

    public AdminGoodsController(AdminGoodsService adminGoodsService) {
        this.adminGoodsService = adminGoodsService;
    }

    /* 获取指定商品编号的商品详细信息 */
    @GetMapping("/edit/detail")
    public Object GoodsDetail(@RequestParam("id") Long goodsId){
        System.out.println(goodsId);
        Goods goods = adminGoodsService.getGoodsById(goodsId);
        List<GoodsAttribute> goodsAttributes = adminGoodsService.getGoodsAttributes(goodsId);
        List<GoodsSpecification> goodsSpecifications = adminGoodsService.getGoodsSpecification(goodsId);
        Map<String,Object> data = new HashMap<>();
        data.put("goods",goods);
        data.put("specifications",goodsSpecifications);
        data.put("attributes",goodsAttributes);
        return ResponseUtil.ok(data);
    }

    /* 修改商品信息 */
    @PostMapping("/edit/update")
    public Object updateGoodsAll(@RequestBody GoodsAllinone goodsAllinone){
        adminGoodsService.updateGoodsAll(goodsAllinone);
        return ResponseUtil.ok();
    }

    /* 新增商品信息 */
    @PostMapping("/create/create")
    public Object createGoodsAll(@RequestBody GoodsAllinone goodsAllinone){
        adminGoodsService.insertGoodsAll(goodsAllinone);
        return ResponseUtil.ok();
    }

    /* 获取商品订单信息 */
    @GetMapping("/order/detail")
    public Object goodsOrderDetail(){
        List<GoodsOrder> goodsOrders = adminGoodsService.getGoodsOrder();
        List<GoodsOrderSpDto> goodsOrderSpDtos = new ArrayList<>();
        for (GoodsOrder goodsOrder:goodsOrders) {
            GoodsOrderSpDto goodsOrderSpDto = new GoodsOrderSpDto();
            Long goodsId = goodsOrder.getGoodsId();
            Long goodsSpecId = goodsOrder.getGoodsSpecId();
            goodsOrderSpDto.setGoodsOrder(goodsOrder);
            Goods goods = adminGoodsService.getGoodsById(goodsId);
            GoodsSpecification goodsSpecification =  adminGoodsService.getSpecificationById(goodsSpecId);
            goodsOrderSpDto.setGoods(goods);
            goodsOrderSpDto.setGoodsOrder(goodsOrder);
            goodsOrderSpDto.setGoodsSpecification(goodsSpecification);
            goodsOrderSpDtos.add(goodsOrderSpDto);
        }
        return ResponseUtil.ok(goodsOrderSpDtos);
    }

    /* 对商品订单进行发货处理 */
    @PostMapping("/order/deliver")
    public Object goodsOrderDeliver(@RequestBody GoodsOrder goodsOrder){
        goodsOrder.setOrderStatus(2);
        adminGoodsService.updateOrderStatus(goodsOrder);
        return ResponseUtil.ok();
    }

    /* 对商品订单进行删除处理 */
    @PostMapping("/order/delete")
    public Object goodsOrderDelete(@RequestBody GoodsOrder goodsOrder){
        adminGoodsService.deleteOrder(goodsOrder.getId());
        return ResponseUtil.ok();
    }
}
